/**
 * Created by chengming on 18/4/13.
 * version 1.0
 */
"use strict";
const schedule = require('node-schedule');
const logger = require('./conf/logger');
const request = require('request-promise');
const ip = require('./util/ip');
const sleep = require('await-sleep');
const report = require('./exception/ErrorReport');
const server = require('./conf/server');
const type = require('./conf/argv')();


const ArticleUpdateDetailAdapter = require('./adapter/ArticleUpdateDetailAdapter');


(async function(){
	
	while(true){
		let echo;
		try{
			echo = await request({
				url: server.addr+'/seed/childTodo/'+type,
				json:true
			});

			if(echo.code != 200){
				await sleep(1000);
			}else{
				if(echo.data) {
                    logger.debug("从子页面队列中获取任务:"+JSON.stringify(echo));
                    //判断要抓取内容类型
                    await new ArticleUpdateDetailAdapter(echo.data, false, false).execute();
				}
			}
		}catch(e){
			logger.error('在处理'+JSON.stringify(echo)+'出错了,5秒后重试');
			if(echo && echo.code == 200){
				report(echo.data.sourceUrl, echo.data.source, echo.data.pageSite, '获取子种子站点异常,异常原因:'+e.message+" \n错误堆栈:"+e.stack);
			}
			await sleep(5000);
		}
	}

})();
